﻿@using Stardust.Data;
@using Stardust.Data.Configs;
@using Stardust.Data.Deployment;
@using Stardust.Data.Monitors;
@{
    var path = Context.Request.Path + "";
    var path2 = "/" + path?.Split("/").Take(3).Join("/");

    var name = "";
    var appId = Context.Request.Query["appId"].ToInt(0);
    var configId = Context.Request.Query["configId"].ToInt(0);
    var deployId = Context.Request.Query["deployId"].ToInt(0);
    var monitorId = Context.Request.Query["monitorId"].ToInt(0);
    if (appId == 0 && path2.EqualIgnoreCase("/Registry/App"))
    {
        appId = Context.Request.Query["Id"].ToInt(0);
    }
    if (appId == 0 && path2.EqualIgnoreCase("/Configs/AppConfig", "/Configs/ConfigData", "/Configs/ConfigHistory"))
    {
        // var id = Context.Request.Query["Id"].ToInt(0);
        var entity = AppConfig.FindById(configId);// ?? AppConfig.FindById(id);
        if (entity != null && entity.AppId > 0) appId = entity.AppId;
    }
    if (appId == 0 && path2.EqualIgnoreCase("/Deployment/AppDeploy", "/Deployment/AppDeployVersion", "/Deployment/AppDeployNode", "/Deployment/AppDeployHistory"))
    {
        // var id = Context.Request.Query["Id"].ToInt(0);
        var entity = AppDeploy.FindById(deployId);// ?? AppDeploy.FindById(id);
        if (entity != null && entity.AppId > 0) { appId = entity.AppId; name = entity.AppName ?? entity.Name; }
    }
    if (appId == 0 && path2.EqualIgnoreCase("/Monitors/AppTracer", "/Monitors/AppDayStat"))
    {
        // var id = Context.Request.Query["Id"].ToInt(0);
        var entity = AppTracer.FindByID(monitorId);// ?? AppTracer.FindByID(id);
        if (entity != null && entity.AppId > 0) { appId = entity.AppId; name = entity.AppName ?? entity.Name; }
    }

    var start = Context.Request.Query["dtStart"].ToDateTime();
    var end = Context.Request.Query["dtEnd"].ToDateTime();
    if (end.Year < 2000) end = Context.Request.Query["dtEnd2"].ToDateTime().AddSeconds(1);

    var app = App.FindById(appId);

    var dic = new Dictionary<String, Object>();
    dic[app?.Name + ""] = "/Registry/App/Edit?Id=" + appId;
    dic["实例"] = "/Registry/AppOnline?appId=" + appId;
    dic["历史"] = "/Registry/AppHistory?appId=" + appId;
    dic["命令"] = "/Registry/AppCommand?appId=" + appId;
    dic["性能"] = "/Registry/AppMeter?appId=" + appId;
    dic["注册中心"] = "/Registry/AppService?appId=" + appId;
    dic["服务消费"] = "/Registry/AppConsume?appId=" + appId;

    var config = AppConfig.FindById(configId);
    if (config == null)
    {
        var configs = AppConfig.FindAllByAppId(appId);
        if (configs.Count == 1) config = configs[0];
    }
    dic["配置中心"] = $"/Configs/AppConfig?appId={appId}&configId={config?.Id}";
    if (config != null)
    {
        dic["配置数据"] = $"/Configs/ConfigData?appId={appId}&configId={config.Id}";
    }

    var deploy = AppDeploy.FindById(deployId);
    if (deploy == null)
    {
        var deploys = AppDeploy.FindAllByAppId(appId);
        if (deploys.Count == 1) deploy = deploys[0];
    }
    dic["发布中心"] = $"/Deployment/AppDeploy?appId={appId}&deployId={deploy?.Id}";
    if (deploy != null)
    {
        dic["上传新版本"] = $"/Deployment/AppDeployVersion?appId={appId}&deployId={deploy.Id}";
        dic["部署发布"] = $"/Deployment/AppDeployNode?enable=1&appId={appId}&deployId={deploy.Id}";
    }

    var monitor = AppTracer.FindByID(monitorId);
    if (monitor == null)
    {
        var monitors = AppTracer.FindAllByAppId(appId);
        if (monitors.Count == 1) monitor = monitors[0];
    }
    dic["监控中心"] = $"/Monitors/AppTracer?appId={appId}&monitorId={monitor?.ID}";
    if (monitor != null)
    {
        dic["监控大屏"] = $"/Monitors/AppDayStat?appId={appId}&monitorId={monitor.ID}";
    }
}
@if (appId > 0)
{
    <div class="navbar-collapse text-center">
        <ul class="nav nav-pills" style="margin-bottom: 10px; display: inline-block;float: none;">
            @foreach (var item in dic)
            {
                if (item.Value is IDictionary<String, Object> childs)
                {
                    <li role="presentation" class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">@item.Key <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            @foreach (var elm in childs)
                            {
                                var url = elm.Value + "";
                                var v = url.Substring(null, "?");
                                if (start.Year > 2000) url += "&dtStart=" + start.ToFullString();
                                if (end.Year > 2000) url += "&dtEnd=" + end.ToFullString();
                                <li role="presentation" class="@(path.EqualIgnoreCase(v)?"active":"")">
                                    <a href="@url">@elm.Key</a>
                                </li>
                            }
                        </ul>
                    </li>
                }
                else
                {
                    var url = item.Value + "";
                    var v = url.Substring(null, "?");
                    if (start.Year > 2000) url += "&dtStart=" + start.ToFullString();
                    if (end.Year > 2000) url += "&dtEnd=" + end.ToFullString();
                    <li role="presentation" class="@(path.EqualIgnoreCase(v)?"active":"")">
                        <a href="@url">@item.Key</a>
                    </li>
                }
            }
        </ul>
    </div>
}